// pages/perCenter/shipAddW/shipAddW.js
var util = require('../../../utils/util.js');
var dateUtil = require('../../../utils/data.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();
Page({
  data: {
    id: '',
    bankName: '',
    accountName: '',
    accountNumber: '',
    accountNumberTail: '',
    logoUrl:'',
    cardType:'',
    bankOfDeposit: '',
    bankCode:'',
    isDefault:false,
    type: 'add',
    token:'',
    disabled:true
  },

  onLoad: function (options) {
    var that= this
    // 页面初始化 options为页面跳转所带来的参数
    var accountNumber = options.accountNumber
    that.setData({
      accountNumber: accountNumber,
      accountNumberTail: accountNumber.substring(accountNumber.length - 4, accountNumber.length)
    })
    wx.showLoading({
      title: '请稍候',
    })
    util.request(api.BankInfo, { cardNo: accountNumber }).then(function (res) {
      wx.hideLoading();
      if (res.code === '200') {
        console.log(res.datas);
        that.setData({
          bankName: res.datas.bankName,
          logoUrl: res.datas.logoUrl,

          cardType: res.datas.cardTypeName,
          bankCode: res.datas.bank
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration:3000
        })
        setTimeout(function () {
          wx.navigateBack({
            
          })
        }, 3000)
        
        return
      }
    });
    
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
 
  setAccountName:function(e){
    var that = this;
    that.setData({
      accountName: e.detail.value
    })
    if (e.detail.value != null && that.data.bankOfDeposit != '') {
      that.setData({
        disabled: false
      })
    } else {
      that.setData({
        disabled: true
      })
    }
  },

   setBankOfDeposit: function (e) {   //获取input输入的值
    var that = this;
    that.setData({
      bankOfDeposit: e.detail.value
    })
    var that = this;
    if (e.detail.value != null && that.data.accountName != '') {
      that.setData({
        disabled: false
      })
    } else {
      that.setData({
        disabled: true
      })
    }
  }, 
  setDefault: function (e) {   //获取input输入的值
    var that = this;
    if (that.data.isdefault == true) {
      that.setData({
        isdefault: false
      })
    } else {
      that.setData({
        isdefault: true
      })
    }
  },
  setLogoUrl:function(){
    var that = this;
    that.setData({
      logoUrl: e.detail.value
    })
  },
  save: function () {
    var that = this;
    var obj = new Object();

    obj.id = that.data.id;
    obj.bankName = that.data.bankName;
    obj.accountName = that.data.accountName;
    obj.accountNumber = that.data.accountNumber;
    obj.bankOfDeposit = that.data.bankOfDeposit;
    obj.logoUrl = that.data.logoUrl;
    obj.bankCode= that.data.bankCode;
    obj.cardType = that.data.cardType;
    obj.isDefault = that.data.isDefault;
    let token = wx.getStorageSync('token');

    if (that.data.accountNumber=="") {
      wx.showToast({
        title: '银行卡号为空',
        icon: 'none'
      })
      return
    }

    if (that.data.bankOfDeposit == "") {
      wx.showToast({
        title: '支行地址为空',
        icon: 'none'
      })
      return
    } if (that.data.accountName == "") {
      wx.showToast({
        title: '帐户名为空',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '保存中',
    })
    util.request(api.BankOper, { token: token, bankCardStr: obj, type: that.data.type }).then(function (res) {
      wx.hideLoading();
      if (res.code === '200') {
       wx.redirectTo({
         url: '/pages/perCenter/bankList/bankList',
       })
      }
    });
  }  
})



